#include <stdlib.h>
#include <stdio.h>
#include <string.h>

// 注释
/**
 * ver: C99
 * gcc: 4.2.1
 */

#define NEWLINE '\n'

void mute(int num, ...)
{
}

void assert(int exp, const char *msg)
{
    if (!exp)
    {
        printf("%s\n", msg);
        exit(0);
    }
}

void vari()
{
    printf("变量常量\n");
    char c = 'a';
    int i = 0;
    auto int ai;     //存储在RAM中
    register int ri; //存储在寄存器中
    float f = 0.0;   //单精度是这样的格式，1位符号，8位指数，23位小数。
    unsigned int ui = 0;
    int *ip = &i;

    const int ci = 1;
}
void stri()
{
    printf("字符串\n");
    char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
    const char *cs = "hellow";
}
void coll()
{
    printf("数组集合\n");
    int is[4] = {1, 2, 3, 0};
    int threedim[5][10][4];

    is[3] = 4;
    assert(is[3] == 4, "is[3] == 4  is error.");
}
void proc()
{
    printf("流程控制\n");

    if (1)
    {
    }
    else if (1) //这第二个 if 实际是被嵌套在第一个 if 语句的else部分
    {
    }
    else
    {
    }

    for (int a = 0; a < 3; a = a + 1)
    {
        mute(1, &a);
    }

    char grade = 'B';
    switch (grade)
    {
    case 'A':
        break;
    case 'B':
    case 'C':
        break;
    case 'D':
        break;
    case 'F':
        break;
    default:;
    }
}

int func_f(int a, int b)
{
    return a + b;
}
int *func_arr(int *arr, int size)
{
    arr[0] = 9;
    return arr;
}

void *func_mem(int size)
{
    int *pi = (int *)malloc(size * sizeof(int));
    pi[0] = 1;
    return pi;
}

void func()
{
    printf("函数方法\n");
    // func_*
    assert(func_f(1, 2) == 3, "func_f(1,2) == 3 is error.");

    typedef int (*fun_ptr)(int, int); // 声明一个指向同样参数、返回值的函数指针类型
    fun_ptr f = func_f;
    assert(f(1, 2) == 3, "f(1,2) == 3 is error.");

    int arr[4];
    assert(func_arr(arr, 4)[0] == 9, "func_arr(arr,4)[0] == 9 is error.");

    int *pi = func_mem(3);
    assert(*pi == 1, "*pi == 1 is error.");
    pi[1] = 2;
    assert(pi[1] == 2, "*pi == 2 is error.");
    int *p3 = pi + sizeof(int);
    *p3 = 3;
    assert(*p3 == 3, "*pi == 3 is error.");
    free(pi);
}
void clas()
{
    printf("类和对象\n");
    struct A
    {
        int a;
    };
    struct A a;
    a.a = 9;
    assert(a.a == 9, "a.a==9 is error");

    struct B
    {
        struct A A;
        const char *b;
    };

    struct B b = {1, "b"};
    assert(b.A.a == 1, "");
    assert(strcmp(b.b, "b") == 0, "");

    struct B *pb = malloc(sizeof(struct B));
    pb->A.a = 1;
    pb->b = "b";
    assert(pb->A.a == 1, "pb->A.a == 1 is error.");
    assert(strcmp(pb->b, "b") == 0, "pb->b == b is error.");
    free(pb);
}

int main()
{
    printf("Hello, World! \n");
    vari();
    stri();
    coll();
    proc();
    func();
    clas();
    return 0;
}
